import { ContactModel } from './ContactModel'
import { OrderItemModel } from './OrderItemModel'

export class OrderModel {
  id: number

  // 订单头
  consumer?: ContactModel
  era: number // 订单归属年份
  batch: number
  date: Date // timestamp

  // 物流
  needDelivery: boolean
  deliveryPerson?: ContactModel
  receiver: string
  fare: number

  // 其他
  invalid: boolean
  settled: boolean
  notes: string
  stub?: string

  // 主体
  items: OrderItemModel[]

  constructor() {
    this.id = 0
    this.consumer = undefined
    this.era = 0
    this.batch = 0
    this.date = new Date()

    this.needDelivery = false
    this.deliveryPerson = undefined
    this.receiver = ''
    this.fare = 0

    this.invalid = false
    this.settled = false
    this.notes = ''
    this.stub = undefined

    this.items = []
  }

  static readonly SERIAL_BASE = 7355608

  public serial() {
    const serial = (OrderModel.SERIAL_BASE + this.id).toString().padStart(8, '0')
    return serial
  }

  public totalAmount(withFare = true) {
    const ta = this.items.map(x => x.amount).reduce((y, x) => y + x, 0)
    return ta + (withFare && this.needDelivery ? this.fare : 0)
  }
}

// export interface LxServerOrder {
//   id: number
//   consumerId: number
//   batch: number
//   date: number // timestamp

//   needDelivery: boolean
//   deliveryPersonId: number | null
//   receiver: string
//   fare: number

//   invalid: boolean
//   settled: boolean
//   notes: string
//   stub: string | null

//   items: string // json format
// }
